package com.lsy.weizhi2.fragment;

import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;


import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.MyImagesAdapter;
import com.lsy.weizhi2.customview.AutoGridView;
import com.lsy.weizhi2.myutils.AblumCall;
import com.lsy.weizhi2.picture.ImageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 嗨哟 on 2016/12/21.
 */
public class MyAblum_frag_list extends Fragment {
    private Context context;
    private View view;
    private AutoGridView myablum_gv;
    private AblumCall ablumCall;
    private List<ImageInfo>imageInfoList;
    private MyImagesAdapter myImagesAdapter;
    private String s="aaaa";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ablumCall= (AblumCall) getActivity();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.ablum,container,false);
        myablum_gv= (AutoGridView) view.findViewById(R.id.myablum_gv);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        Bundle bundle=getArguments();
        if(bundle!=null){
            String name=bundle.getString("name");
            int count=bundle.getInt("count");
            imageInfoList=getImagesByBucketDisplayName(name,count);
            myImagesAdapter=new MyImagesAdapter(context,imageInfoList);
            myablum_gv.setAdapter(myImagesAdapter);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        myablum_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String path=imageInfoList.get(i).getPath();
                ablumCall.ImagePath(path);
            }
        });
    }

    private List<ImageInfo> getImagesByBucketDisplayName(String displayName, int id){
        List<ImageInfo>imageInfos=new ArrayList<>();
        ContentResolver cr=context.getContentResolver();
        String[]projection={MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA};
        String selection=null;
        if(TextUtils.isEmpty(displayName)){
            //没有传入文件夹名称，查询所有图片信息
            if(id>0){
                selection= MediaStore.Images.Media._ID+"<"+id;
            }
        }else{
            selection= MediaStore.Images.Media.BUCKET_DISPLAY_NAME+"=\""+displayName+"\" and ("+MediaStore.Images.Media.MIME_TYPE+"=\"image/jpeg\" or "+MediaStore.Images.Media.MIME_TYPE+"=\"image/png\")";
        }
        Cursor cursor=cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection,selection,null, MediaStore.Images.Media._ID+" desc");
        if(cursor.moveToFirst()){
            do{
                ImageInfo imageInfo=new ImageInfo();
                //图片信息对象
                int _id=cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
                imageInfo.setId(_id);
                //MyMethod.showToastStr(context,""+_id);
                //获取图片缩略图
                String[]projectionThumb={MediaStore.Images.Thumbnails.DATA};
                Cursor cursorprojectionThumb=cr.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,projectionThumb, MediaStore.Images.Thumbnails.IMAGE_ID+"="+_id+"",null,null);
                String path=null;
                if(cursorprojectionThumb.moveToFirst()){
                    //存在缩略图
                    path=cursorprojectionThumb.getString(cursorprojectionThumb.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
                }
                if(cursorprojectionThumb!=null){
                    cursorprojectionThumb.close();
                }
                String bigPath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                if(TextUtils.isEmpty(path)){
                    path=bigPath;
                }
                imageInfo.setPath(path);
                imageInfo.setBucketDisplayName(displayName);
                imageInfo.setBigpath(bigPath);
                imageInfos.add(imageInfo);
                //show保存信息
                //MyMethod.showToastStr(context,path+"\n"+displayName+"\n"+bigPath);
            }while (cursor.moveToNext());
        }
        if(cursor!=null){
            cursor.close();
        }
        if(cr!=null){
            cr=null;
        }
        return imageInfos;
    }
    
}
